package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据手机号码从数据库中查询有无对应记录
     * @param phone
     * @return true：该手机号已经注册  false:该手机号未被注册
     */
    @Override
    public Boolean checkPhone(String phone) {
        int count = userInfoMapper.selectCountByPhone(phone);
        return count > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //生成验证码内容
        String code = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        //生成短信内容
        StringBuilder sb = new StringBuilder(100);
        sb.append("您的验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内使用。");
        System.out.println(sb.toString());
        //把手机号码和验证码缓存到redis中
        userInfoRedisService.setVerifyCode(phone,code);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException{
        //判断参数是否为空
        AssertUtil.hasLength(phone,"手机号不能为空");
        AssertUtil.hasLength(nickname,"昵称不能为空");
        AssertUtil.hasLength(password,"密码不能为空");
        AssertUtil.hasLength(rpassword,"确认密码不能为空");
        AssertUtil.hasLength(verifyCode,"验证码不能为空");
        //判断两次密码输入是否一致
        AssertUtil.isEquals(password,rpassword,"密码与确认密码输入不一致");
        //判断手机号是否已被注册
        if(this.checkPhone(phone)){
            throw new LogicException("手机号已被注册");
        }
        //判断验证码是否过期或错误
        String code = userInfoRedisService.getVerifyCode(phone);
        if(code == null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码错误或者失效");
        }
        UserInfo user = new UserInfo();
        user.setNickname(nickname);
        user.setLevel(1);
        user.setPassword(password);
        user.setPhone(phone);
        user.setState(UserInfo.STATE_NORMAL);
        user.setHeadImgUrl("images/default.png");
        userInfoMapper.insert(user);
    }

    @Override
    public String userLogin(String username, String password) {
        //通过username和password获取当前请求登录的用户对象
        UserInfo user = userInfoMapper.selectUserByUsernameAndPassword(username,password);
        //判断用户对象是否为空
        if (user == null) {
            throw new LogicException("账户不存在或密码错误");
        }
        //把用户对象缓存到redis中
        String token = UUID.randomUUID().toString().replaceAll("-","");
        userInfoRedisService.setUserLoginToken(token,user);
        return token;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
